/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.chlitina.store.modules.base.service;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.chlitina.store.common.persistence.Page;
import com.chlitina.store.common.service.BaseService;
import com.chlitina.store.modules.base.dao.CustomerGroupDao;
import com.chlitina.store.modules.base.entity.CustomerGroup;
import com.chlitina.store.modules.base.entity.CustomerGroupDetail;
import com.chlitina.store.modules.base.entity.CustomerGroupExport;
import com.chlitina.store.modules.sys.utils.StoreUtils;
import com.google.common.collect.Lists;

/**
 * 客户组管理Service
 * @author lvbo
 * @version 2013-07-23
 */
@Component
@Transactional(readOnly = true)
public class CustomerGroupService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(CustomerGroupService.class);
	
	@Autowired
	private CustomerGroupDao customerGroupDao;
	
	public CustomerGroup get(Long id) {
		return customerGroupDao.findOne(id);
	}
	
	public Page<CustomerGroup> find(Page<CustomerGroup> page, CustomerGroup customerGroup) {
		DetachedCriteria dc = customerGroupDao.createDetachedCriteria();
		if (customerGroup.getStore()!=null && customerGroup.getStore().getId()>0){
			dc.add(Restrictions.eq("store.id", customerGroup.getStore().getId()));
		}
		if (customerGroup.getGroupName() != null){
			dc.add(Restrictions.like("groupName", "%" + customerGroup.getGroupName() + "%"));
		}
		dc.addOrder(Order.desc("id"));
		return customerGroupDao.find(page, dc);
	}
	public List<CustomerGroupExport> export(String customerGroup) {
		customerGroupDao.clear();
		DetachedCriteria dc = customerGroupDao.createDetachedCriteria();
		dc.add(Restrictions.eq("store.id", StoreUtils.getStore().getId()));
		dc.add(Restrictions.like("groupName", "%" + customerGroup + "%"));
		dc.addOrder(Order.desc("id"));
		List<CustomerGroupExport> cgeList = Lists.newArrayList();
		List<CustomerGroup> list = customerGroupDao.find(dc);
		for (CustomerGroup cg : list) {
			List<CustomerGroupDetail> customerGroupDetailList = cg.getCustomerGroupDetailList();
			if (customerGroupDetailList != null && customerGroupDetailList.size() == 0) {
				CustomerGroupExport groupNode = new CustomerGroupExport();
				groupNode.setGroupName(cg.getGroupName());
				cgeList.add(groupNode);
			}
			for (CustomerGroupDetail cgd : customerGroupDetailList) {
				CustomerGroupExport cge = new CustomerGroupExport();
				cge.setGroupName(cg.getGroupName());
				cge.setCustomerName(cgd.getCustomer().getName());
				cge.setCustomerMobile(cgd.getCustomer().getMobile());
				cgeList.add(cge);
			}
		}
		return cgeList;
	}
	
	@Transactional(readOnly = false)
	public List<CustomerGroup> getByStore(Long storeId) {
		DetachedCriteria dc = customerGroupDao.createDetachedCriteria();
		dc.add(Restrictions.eq("store.id", storeId));
		 return customerGroupDao.find(dc);
	}
	@Transactional(readOnly = false)
	public void save(CustomerGroup customerGroup) {
		customerGroupDao.save(customerGroup);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		customerGroupDao.delete(id);
	}
	
}
